import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/LoginPage'
import TypingGame from '@/views/TypingGame'
import MyHome from '@/views/MyHome'
import MainPage from '@/views/MainPage'
import Users from '@/views/Menus/MyUsers'
import Order from '@/views/Menus/MyOrder'
import AdministratorLogin from '@/views/AdministratorLogin/AdministratorLogin'
import Rank from '@/views/Rank/RankPage'
import Register from '@/views/RegisterPage'
Vue.use(VueRouter)

const routes = [
  // { path: '/register', component: Register },
  { path: '/register', component: Register },
  // { path: '/', redirect: '/rank' },
  { path: '/rank', component: Rank },
  // { path: '/', redirect: '/administrator-login' },
  { path: '/administrator-login', component: AdministratorLogin },
  { path: '/', redirect: '/home' },
  { path: '/home', component: MyHome },
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  // { path: '/', redirect: '/typing-game' },
  { path: '/typing-game', component: TypingGame },
  // { path: '/', redirect: '/main' },
  {
    path: '/main',
    component: MainPage,
    redirect: '/main/users',
    children: [
      // 子路由规则不能写 /
      { path: 'users', component: Users },
      { path: 'order', component: Order }
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  // base: ''
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && to.path !== '/home' && to.path !== '/register' && to.path !== '/administrator-login') {
    // 要访问后台主页 需要判断是否有 tag
    const tag = localStorage.getItem('tag')
    // 如果tag值存在 则放行
    if (tag) {
      next()
      // 没有tag 则直接强制跳转到登录页
    } else {
      next('/home')
    }
  } else {
    next()
  }
})
export default router
